<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="content-type" content="text/html; charset=utf-8">
    <meta name="renderer" content="webkit"/>
    <meta name="force-rendering" content="webkit"/>
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"/>
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="stylesheet" href="../css/shop.css">
    <link rel="stylesheet" href="../css/weui.min.css">
    <link rel="stylesheet" href="../css/jquery-weui.css">
    <script src="../js/jquery-2.1.4.js"></script>
    <script src="../js/jquery-weui.js"></script>
    <script src="../wxJs/utils/contain.js"></script>
    <script src="../wxJs/utils/storage.js"></script>
</head>
<style>
    .weui-btn_warn {
        background: rgba(29, 108, 247, 0.8);
    }

    .model1 {
        position: fixed;
        top: 0;
        left: 0;
        height: 100%;
        width: 100%;
        background-color: rgba(0, 0, 0, .6);
        display: none;
    }

    .model1 * {
        box-sizing: border-box
    }

    .model1-contain {
        position: absolute;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -50%);
        background-color: #fff;
        border-radius: 8px;
        overflow: hidden
    }

    .model1-title {
        width: 100%;
        background-color: #fff;
        border-bottom: 1px solid #d3d3d3;
        text-align: center;
        padding: 15px 0;
        font-weight: 700;
        line-height: 1
    }

    .model1-content {
        border-top: 1px solid #d3d3d3
    }

    .model1-content input[type='password'] {
        padding: 20px;
        text-align: center;
        cursor: none;
        border: none;
        outline: 0;
        font-size: 18px
    }

    .model1-footer {
        display: flex;
        text-align: center;
        border-top: 1px solid #d3d3d3;
        font-size: 16px
    }

    .model1-footer * {
        flex: 1;
        padding: 10px 0;
        cursor: pointer
    }

    .model1-footer .confirm {
        background-color: #ff9c1c;
        color: #fff
    }
</style>

<body>
<div class="withdrawal-header">
    <div class="header" style="display: flex">
        <div class="haader-left" href="javascript:window.history.go(-1)"></div>
        <div class="haader-content">提现</div>
        <div class="haader-right" id="withdrawal_log">

        </div>
    </div>
    <div class="withdrawal-money">0.00</div>
    <div class="withdrawal-balance">账户余额(元)</div>
</div>

<div class="withdrawal-contaion">
    <div class="withdrawal-form">
        <input type="number" maxlength="6" placeholder="提款金额" id="pickMoney" oninput="inpMoney(this)">
        <!-- <div class="btn-all">全部</div>-->
    </div>
    <div class="withdrawal-tips">手续费每笔<em id="shopAdded"></em>元,预计<em>24小时内</em>到账.</div>
</div>
<div class="cell all-w">
    <label for="">收款类型</label>
    <div class="footer js_choose">
        <span id="js_type">
            请选择收款类型
        </span>
        <i class="iconfont icon-right"></i>
    </div>
</div>
<div class="cell all-w" id="js_bank" style="display: none">
    <label for="">银行卡</label>
    <div class="footer js_chooseBank">
        <span>
            请选择银行卡
        </span>
        <i class="iconfont icon-right"></i>
    </div>
</div>
<div class="cell all-w">
    <!-- <label for="">备注</label>
     <div class="footer">
         <input type="text" placeholder="提现备注">
     </div>-->
</div>
<div class="btn-contain">
    <a href="javascript:;" class="weui-btn weui-btn_warn mt10">提现</a>
</div>
<div class="model1">
    <div class="model1-contain">
        <div class="model1-title">支付密码</div>
        <div class="model1-content">
            <input id="code" type="password" placeholder="请输入支付密码" maxlength="6" oninput="setNumType(this)">
        </div>
        <div class="model1-footer">
            <div class="cancel">取消</div>
            <div class="confirm">确认</div>
        </div>
    </div>
</div>
</body>
<script>
    var shopId = GetQueryString("shopId");
    var bankId = "";
    var shopSysId;
    var type;   //1-零钱  2-银行卡
    var isBuTie;   //是否补贴
    if (!shopId) {
        if (!shopId) {
            window.history.back(-1);
        }
    } else {
        $('#withdrawal_log').html('<a href="withdrawal_log.html?shopId=' + shopId + '"><i class="iconfont icon-lianluojilu" style="font-size: 24px;"></i></a>')
        /*查询商户余额*/
        $.post(getWeiContextPath() + "/wx/shop/queryShopMoney", {shopId: shopId}, function (res) {
            $(".withdrawal-money").text(res.shopMoney)
            $("#shopAdded").text(res.shopAdded)
            shopSysId = res.shopSysId
            isBuTie = res.isBuTie
        })
    }


    /*选择银行卡*/
    $(".js_chooseBank").click(function () {
        var $this = $(this);
        var arry = [];
        bankId = ""
        $.post(getWeiContextPath() + "/wx/shop/queryShopBank", {belongId: shopId, belongType: 1}, function (res) {
            console.log(res)
            $.each(res.belongBanks, function (i, obj) {
                var str4 = obj.bankNum.substring(obj.bankNum.length - 4, obj.bankNum.length);
                arry.push({
                    text: obj.bankName + "(" + str4 + ")",
                    onClick: function () {
                        $this.find("span").text(obj.bankName + "(" + str4 + ")")
                        bankId = obj.id
                    }
                })
            })
            $.actions({
                actions: arry
            });
        })


    })

    /*全部*/
    $('.btn-all').on('click', function () {
        $('#pickMoney').val($('.withdrawal-money').text())
    })
    /*提现类型*/
    $(".js_choose").click(function () {
        $.actions({
            actions: [{
                text: "零钱",
                onClick: function () {
                    $("#js_type").text("零钱");
                    type = 1
                    $("#js_bank").hide()
                }
            }, {
                text: "银行卡",
                onClick: function () {
                    $("#js_type").text("银行卡")
                    type = 2
                    $("#js_bank").show()
                    $(".js_chooseBank").click()
                }
            }]
        });
    })

    var pickMoney    //提现实际金额
    var userInfo
    var deductMoney = 0;     //扣除商铺账户余额
    var bankProcedure = 0;   //提现银行卡手续费
    $('.mt10').on('click', function () {

        if (Number($('#pickMoney').val()) > 0) {
            pickMoney = parseFloat($('#pickMoney').val())
        } else {
            $.toast("请输入正确的金额", "forbidden")
            return false;
        }
        if (!type) {     //1-零钱  2-银行卡
            $.toast("提现类型未选择", "forbidden")
            return false;
        }
        var allMoney = parseFloat($('.withdrawal-money').text())    //商户余额
        var shopAdded = parseFloat($("#shopAdded").text())         //额外手续费

        if (type == 2) {    //提现银行卡
            //计算手续费
            if (pickMoney <= 100) {
                bankProcedure = 0.1
            } else {
                bankProcedure = Math.round(pickMoney * 0.1) / 100
            }
            if (isBuTie) {    //补贴：扣除金额不加手续费
                deductMoney = pickMoney + shopAdded
            } else {     //不补贴：扣除金额加手续费
                deductMoney = pickMoney + shopAdded + parseFloat(bankProcedure);
            }
        } else {    //提现零钱
            deductMoney = pickMoney + shopAdded
        }
        $.alert(deductMoney, "需要扣除账户余额", function () {
            if (deductMoney > allMoney) {
                $.toast("余额不足", "forbidden")
                return false;
            }
            /*加入支付密码*/
            userInfo = getUserInfo(shopSysId);
            if (!userInfo.payNumber) {
                $.toast("请先设置支付密码", "forbidden")
                return false;
            } else {
                $('.model1').show()
            }
        })
    })


    /*弹出输入框,判断输入字符返回*/
    $(".confirm").on('click', function () {
        var code = $('#code').val()
        if (!code) {
            $.toast("请输入密码", "forbidden")
            return false;
        }
        if (code == userInfo.payNumber) {
            $('.model1').hide()
            if ($("#js_type").text() == "零钱") {
                $.post(getWeiContextPath() + "/wx/shop/toPickWechat", {
                    pickMoney: pickMoney,
                    shopId: shopId,
                    deductMoney: deductMoney,
                    bankProcedure: bankProcedure
                }, function (res) {
                    if (res.code == 0) {
                        $.toast("提现成功", 1000)
                        $(".withdrawal-money").text(res.data)
                    } else {
                        $.toast(res.msg, "forbidden", function () {
                            window.history.back(-1);
                        });
                    }
                })
            } else if ($("#js_type").text() == "银行卡") {
                if (!bankId) {
                    $.toast("银行卡未选择", "forbidden")
                    return false;
                }
                $.post(getWeiContextPath() + "/wx/shop/toPickBank", {
                    pickMoney: pickMoney,
                    shopId: shopId,
                    bankId: bankId,
                    deductMoney: deductMoney,
                    bankProcedure: bankProcedure
                }, function (res) {
                    if (res.code == 0) {
                        $.toast("提现成功", 1000)
                        $(".withdrawal-money").text(res.data)
                    } else {
                        $.toast(res.msg, "forbidden", function () {
                            window.history.back(-1);
                        });
                    }
                })

            } else {
                $.toast("提现类型未选择", "forbidden")
            }


        } else {
            $.toast("密码错误", "forbidden")
        }
    })

    $(".cancel").on('click', function () {
        $('.model1').hide()
    })

    /*只能输入数字*/
    function setNumType(_this) {
        var num = $(_this).val()
        $(_this).val(num.replace(/[^\d]/g, ''))
    }

    /*只能输入整数或者小数点后两位*/
    function inpMoney(_this) {
        var num = $(_this).val()
        $(_this).val(ChangeNumValue(num))
    }

    function ChangeNumValue(tmpVal) {
        if (tmpVal) {
            var tmpVal = tmpVal.replace(/[^\d\.]/g, '');
            var reg = /^(0|([1-9]\d*))(\.\d{1,2})?$/; //正则验证保留 最多允许后输入两位小数
            if (!reg.test(tmpVal)) {
                tmpVal = tmpVal + "";
                tmpVal = tmpVal.substring(0, tmpVal.indexOf(".") + 3);
                var n = (tmpVal.split('.')).length - 1;
                if (n > 1) {
                    tmpVal = tmpVal.substring(0, tmpVal.indexOf("."));
                }
            }
            return tmpVal;
        } else {
            return "";
        }
    }
</script>
</html>